<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img
          src="http://ww2.sinaimg.cn/large/005Lrp5Lgw1eu1bvrzwafj31kw2jb7wh.jpg"
          alt="杨幂"
          title="杨幂"
        />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" size="medium" @click="removelogin">退出</el-button>
    </el-header>
    <!-- 内容 -->
    <el-container>
      <!-- 侧边栏 -->
      <!-- router是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
      <el-aside width="200px">
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) of menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item1.path"
              v-for="(item1, index1) of item.children"
              @click="saveNavState(`/${item1.path}`)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      //   左侧菜单的一级选项图标
      iconsObj: {
        "125": "iconfont icon-wodedangxuan",
        "103": "iconfont icon-quanxianfenji",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-weibiaoti--",
        "145": "iconfont icon-shuju"
      },
      //   被激活的左侧导航链接地址
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    removelogin() {
      window.sessionStorage.removeItem("token");
      //   点击退出清除左侧的导航激活路径
      window.sessionStorage.removeItem("activePath");
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data } = await this.$http.get("/menus");
      console.log(data.data);
      if (data.meta.status !== 200) return this.$message.err(data.meta.msg);
      this.menuList = data.data;
    },
    saveNavState(activePath) {
      console.log(activePath);
      window.sessionStorage.setItem("activePath", activePath);
    }
  }
};
</script>
<style lang="less" scope>
.home_container {
  height: 100% !important;
  .el-header {
    background-color: #b3c0d1;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    justify-content: space-between;
    padding-left: 0 !important;
    div {
      //   width: 200px;
      height: 100%;
      border-radius: 50%;
      padding: 0 10px;
      display: flex;

      img {
        height: 100%;
        width: 5%;
        border-radius: 50%;
      }
    }
  }

  .el-aside {
    background-color: #d3dce6;
    color: #333;
    text-align: center;
    line-height: 200px;
    i {
      margin-right: 10px;
    }
    .el-menu {
      border-right: 0 !important;
    }
  }

  .el-main {
    background-color: #e9eef3;
    color: #333;
    text-align: center;
    // line-height: 160px;
  }
}
</style>